﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using NextCMS.Models;

public partial class ContentSettings : WebadminBasePage
{
    int id = 0;
    int PageModuleId = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        int.TryParse(Convert.ToString(Request.QueryString["Id"]), out id);
        int.TryParse(Convert.ToString(Request.QueryString["PageModuleId"]), out PageModuleId);
        if (!IsPostBack)
        {
            BindDropdown();
            GetData();
        }
        if (id > 0)
            lnkEditContent.Attributes.Add("onclick", "javascript:window.parent.openWindow('ContentEdit.aspx?Id=" + id + "&PageModuleId=" + PageModuleId + "',{width:790,height:450,center:true});");
        else
        {
            lnkEditContent.Visible = false;
            btnAddContent.Visible=true;
        }
    }
    void BindDropdown()
    {
        PageContentsHelper pcc = new PageContentsHelper();
        
        if (id > 0)
            trSelectContent.Visible = false;
        else
        {
            DDLContent.DataSource = pcc.GetGeneralContents(UserLanguageId);
            DDLContent.DataTextField = "Name";
            DDLContent.DataValueField = "Id";
            DDLContent.DataBind();
        }
        DDLContent.Items.Insert(0, new ListItem("New Content", "0"));
        DDLContent.SelectedValue = id.ToString();
    }
    void GetData()
    {
        

        int contentId = Convert.ToInt32(DDLContent.SelectedValue);
        if (contentId == 0)
            contentId = id;
        if (contentId > 0)
        {
            PageContentsHelper pcc = new PageContentsHelper();
            PageContentModel pcm = pcc.GetById(contentId, UserLanguageId);

            txtContentName.Text = pcm.Name;
            chkIsGeneral.Checked = pcm.IsGeneral;
            if (!String.IsNullOrEmpty(pcm.Content))
            {
                elm1.InnerHtml = pcm.Content;
                txtContent.Text = pcm.Content;
            }
            else
            {
                elm1.InnerHtml = "No Content";
                txtContent.Text = "No Content";
            }
            lnkEditContent.Visible = true;
            btnAddContent.Visible = false;
            lnkEditContent.Attributes.Add("onclick", "javascript:window.parent.openWindow('ContentEdit.aspx?Id=" + contentId + "&PageModuleId=" + PageModuleId + "',{width:790,height:450,center:true});");

        }
        else
        {
            elm1.InnerHtml = "";
            txtContent.Text = "";
            chkIsGeneral.Checked = false;
            lnkEditContent.Visible = false;
            btnAddContent.Visible = true;
        }

    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        PageModulesHelper pmc = new PageModulesHelper();
        PageModuleModel pmm = pmc.GetById(PageModuleId);

        int contentId = Convert.ToInt32(DDLContent.SelectedValue);
        if (contentId > 0)
        {
            if (pmm.RefId == 0)
            {
                pmm.RefId = contentId;
                pmc.Edit(pmm);
            }
        }
        else
        {
            if (contentId == 0)
                contentId = id;
            PageContentsHelper pcc = new PageContentsHelper();
            PageContentModel pcm = pcc.GetById(contentId, UserLanguageId);
            if (pcm == null)
                pcm = new PageContentModel();
            if (Request.Form["elm1"] != null)
                pcm.Content = Convert.ToString(Request.Form["elm1"]);
            else
                pcm.Content = txtContent.Text;
            pcm.IsGeneral = chkIsGeneral.Checked;
            pcm.Name = txtContentName.Text;
            pcm.LanguageId = UserLanguageId;
            
            if (contentId > 0)
                pcc.Edit(pcm);
            else
                contentId = pcc.Save(pcm);



            if (pmm.RefId == 0)
            {
                pmm.RefId = contentId;
                pmc.Edit(pmm);
            }
            if (!String.IsNullOrEmpty(pcm.Content))
            {
                elm1.InnerHtml = pcm.Content;
                txtContent.Text = pcm.Content;
            }
            else
            {
                elm1.InnerHtml = "No Content";
                txtContent.Text = "No Content";
            }
        }
        
        Page.ClientScript.RegisterStartupScript(Page.GetType(), "d", "window.parent.closeDialogWindow();", true);
    }

    protected void DDLContent_SelectedIndexChanged(object sender, EventArgs e)
    {
        GetData();
    }
    protected void btnAddContent_Click(object sender, EventArgs e)
    {
        PageModulesHelper pmc = new PageModulesHelper();
        PageModuleModel pmm = pmc.GetById(PageModuleId);


        PageContentsHelper pcc = new PageContentsHelper();
        PageContentModel pcm = new PageContentModel();
        pcm.IsGeneral = chkIsGeneral.Checked;
        pcm.LanguageId = UserLanguageId;
        pcm.Name = txtContentName.Text;
        pcm.Id = pmm.RefId;
        int newId = pcc.Save(pcm);

        
        if (pmm.RefId == 0)
        {
            pmm.RefId = newId;
            pmc.Edit(pmm);
        }
        Page.ClientScript.RegisterStartupScript(Page.GetType(), "ddd", "window.parent.openWindow('ContentEdit.aspx?Id=" + newId + "&PageModuleId=" + PageModuleId + "',{width:790,height:450,center:true});window.parent.closeDialogWindow();", true);
    }
}